module Quickstep::AdminViewHelper

  def admin_main_nav(role="admin")
    render partial: "admin/navs/#{role}_main_nav"
  end

  def ibox(html_attrs={}, &block)
    content_tag(:div, id: html_attrs[:id], class: "ibox #{html_attrs[:class]}", &block)
  end

  def ibox_title(name, &block)
    concat <<-EOF.html_safe
      <div class="ibox-title">
        <h5>#{name}</h5>
        <div class="ibox-tools">
    EOF

    yield if block_given?

    <<-EOF.html_safe
        </div>
      </div>
    EOF
  end

  def ibox_content(html_attrs={}, &block)
    content_tag(:div, id: html_attrs[:id], class: "ibox-content #{html_attrs[:class]}", &block)
  end

  def admin_table(html_attrs={}, &block)
    content_tag(:div, class: 'table-responsive') do
      content_tag(:table, id: html_attrs[:id], class: "table admin-table #{html_attrs[:table_class]||html_attrs[:class]}", data: html_attrs[:data], &block)
    end
  end

  def light_button_to(label, path, icon = nil, opts = {})
    color = opts[:color]
    link_to(path, opts.merge(class: "btn btn-#{color || 'white'} btn-sm #{opts[:class]}")) do
      if icon
        (content_tag(:i, "", class: "fa-#{icon} fa") + ' ' + label)
      else
        label
      end
    end
  end

  def light_method_button_to(label, path,  icon = nil, method= 'post', data = nil,  color = nil)
    link_to(path, class: "btn btn-#{color || 'white'} btn-sm", method: method, data: data) do
      if icon
        (content_tag(:i, "", class: "fa-#{icon} fa") + ' ' + label)
      else
        label
      end
    end
  end

  def light_blank_button_to(label, path,  icon = nil,  color = nil)
    link_to(path, class: "btn btn-#{color || 'white'} btn-sm", target: '_blank') do
      if icon
        (content_tag(:i, "", class: "fa-#{icon} fa") + ' ' + label)
      else
        label
      end
    end
  end

  def light_detail_button(label, path)
    light_button_to(label, path, 'file-text-o')
  end

  def light_edit_button(label, path)
    light_button_to(label, path, 'pencil')
  end

  def light_pause_button(label, path)
    light_button_to(label, path, 'pause')
  end

  def light_resume_button(label, path)
    light_button_to(label, path, 'play')
  end

  def light_search_button
    # content_tag :button, class: 'btn btn-white btn-sm', type: 'submit' do
    #   content_tag(:i, "", class: 'fa fa-search') + ' 搜索'
    # end
    '<button class="btn btn-primary btn-sm" type="submit"><i class="fa fa-search"></i> 搜索</button>'.html_safe
  end

  def radio_btn_group(field_name, options, value='')
    value = '' if value.nil?
    content_tag 'div', class: "btn-group", data: {toggle: 'buttons'} do
      options.collect do |option|
        option = [option, option] unless option.is_a? Array
        checked = value == option[1].to_s
        content_tag'label', class: "btn btn-sm btn-white #{'active' if checked}" do
          radio_button_tag(field_name, option[1], checked)+ option[0]
        end
      end .join.html_safe
    end
  end

  def admin_form(&block)
    content_tag(:div, class: 'col-sm-8 col-sm-offset-2') do
      ibox(class: 'float-e-margins') do
        ibox_content(&block)
      end
    end
  end

  def admin_form_btn(obj)
    content_tag(:div, class: 'row') do
      content_tag(:div, class: 'col-sm-8 col-sm-offset-2') do
        if obj.new_record?
          content_tag(:button, type: 'submit', class: 'btn btn-primary btn-block') do
            '新建'
          end
        else
          content_tag(:button, type: 'submit', class: 'btn btn-primary btn-block') do
            '保存'
          end
        end
      end
    end
  end

  def admin_form_tab(ids=[])
    content_tag(:ul, class: 'nav nav-tabs margin-top-20 margin-bottom-20') do
      ids.each_with_index do |id, i|
        if i == 0
          content_tag(:li, class: 'active') do
            link_to("##{id[:key]}", id: id[:value])
          end
        else
          content_tag(:li) do
            link_to("##{id[:key]}", id: id[:value])
          end
        end
      end
    end
  end

  def statistic_modal_widget(url, html_attrs={col: 'col-lg-6', bg: 'navy-bg'}, &block)
    concat <<-EOF.html_safe
      <div class="#{html_attrs[:col]}">
        <div class="widget style1 #{html_attrs[:bg]} modal-widget" data-url="#{url}" data-toggle="modal" data-target="#detail-modal" style="cursor:pointer;">
          <div class="row">
    EOF

    yield if block_given?

    <<-EOF.html_safe
          </div>
        </div>
      </div>
    EOF
  end

  def statistic_widget(html_attrs={col: 'col-lg-6', bg: 'navy-bg'}, &block)
    concat <<-EOF.html_safe
      <div class="#{html_attrs[:col]}">
        <div class="widget style1 #{html_attrs[:bg]}">
          <div class="row">
    EOF

    yield if block_given?

    <<-EOF.html_safe
          </div>
        </div>
      </div>
    EOF
  end
end
